from django import forms
from .models import Profile

class ProfileForm(forms.ModelForm):
    """
    ProfileForm 用于创建和更新 Profile 模型实例的表单
    """
    class Meta:
        model = Profile
        fields = [
            'nickname', 'full_name', 'education', 'interested_position',
            'blog_or_repo_link', 'resume', 'work_experience', 'job_title'
        ]
        widgets = {
            'work_experience': forms.Textarea(attrs={'rows': 3, 'cols': 40}),
            'job_title': forms.TextInput(attrs={'size': 40}),
        }

    def clean(self):
        """
        自定义表单验证逻辑
        """
        cleaned_data = super().clean()
        company = cleaned_data.get("company")
        work_experience = cleaned_data.get("work_experience")
        job_title = cleaned_data.get("job_title")

        # 用户未加入企业时，确保工作经验和岗位信息为空
        if not company:
            if work_experience or job_title:
                raise forms.ValidationError("You can only fill work experience and job title if you are part of a company.")
        return cleaned_data
